
#ifndef __MEDIA_TCP_CLIENT_H__
#define __MEDIA_TCP_CLIENT_H__

#include <map>
#include <mutex>
#include <string>
#include <functional>
#include "json.h"
#include "Thread/Thread.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include "Poller/Timer.h"



namespace Media{

class MediaTcpClient{
public:
    const int ConnectPort = 2203;
    
public:

	static MediaTcpClient *instance(std::string svrAddr);

    bool creatSocketConnect(uint16_t port);

    bool destroySocketConnect(uint16_t port);

    bool sendKeepLive();
	
private:
	MediaTcpClient(std::string svrAddr);

	~MediaTcpClient();

	void ThreadProc(Fdt::Thread *thread);

	bool detectConnectIsConnect();

	void connectSocketThread(Fdt::Thread *thread);

	bool sendMessage(const std::string& strMsg);

	bool keepLiveTimeout();


private:
    const uint16_t                                          RECEIVE_LEN = 1024;
    
	static std::map<std::string, MediaTcpClient *>          sm_objectMap;

	static std::recursive_mutex 			                sm_mapMutex;

    int                                                     m_clientSockfd = -1;

    bool                                                    m_connetStatus = false;

    std::string                                             m_svrAddr;

    uint16_t                                                m_svrPort;                

    std::mutex                                              m_connectMutex;

    struct sockaddr_in                                      m_svrSocketInfo;

    std::shared_ptr<Fdt::Timer>                             m_keepLiveTimer;

    Fdt::Thread 						                    m_threadConnect{"MediaTcpConnect"};
	
	Fdt::Thread 						                    m_thread{"MediaTcpClient"};//thread放在最后,保证thread最后构造,最先析构

};

}//namespace Media
#endif


